#include "servermenu.h"

ServerMenu::ServerMenu(QString &name, quint16 id, QObject *parent)
	: QMenu(name), _id(id), _mode(false)
{
	init();
	connectMode();
}

ServerMenu::~ServerMenu()
{
	delete _connect;
	delete _config;
}

void							ServerMenu::init()
{
	QIcon						iconfig("config.ico");
	
	_connect = new QAction("Connect", this);
	_config = new QAction("Config", this);
	_connected = new QIcon("speaker-64.ico");
	_disconnected = new QIcon("mute-64.ico");

	addAction(_connect);
	addAction(_config);
	
	_config->setIcon(iconfig);

	setStyleSheet("border: 3px solid lightblue;");

	connect(_connect, SIGNAL(triggered()), this, SLOT(changeMode()));
	connect(_config, SIGNAL(triggered()), this, SLOT(handleConfig()));
}

quint16							ServerMenu::getId() const
{
	return _id;
}

void							ServerMenu::setId(quint16 id)
{
	_id = id;
}

void							ServerMenu::changeName(QString &name)
{
	setTitle(name);
}

void							ServerMenu::connectMode()
{
	// css : background-color = red;
	_mode = false;
	_connect->setText("On");
	setIcon(*_disconnected);
}

void							ServerMenu::disconnectMode()
{
	// css : background-color = green;
	_mode = true;
	_connect->setText("Off");
	setIcon(*_connected);
}

/*public slots:*/
void							ServerMenu::changeMode()
{
	if (_mode)
		connectMode();
	else
		disconnectMode();
	emit changeMode(_id, _mode);
}

void							ServerMenu::handleConfig()
{
	emit config(_id);
}